////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft Corporation.  All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "tinyhal.h"
#include "..\..\..\..\..\DeviceCode\Targets\Native\FM3\DeviceCode\FM3.h"
//#include "mcu.h"

#define CQFM3DUINO  0
#define BOARD   CQFM3DUINO

// CQFM3DUINO
// P09  RESET

#if (BOARD == CQFM3DUINO)
#define LCD_ADDRESS (*(volatile unsigned short*)0x61000000)
#define LCD_VALUE   (*(volatile unsigned short*)0x61020000)
#else
#define LCD_ADDRESS (*(volatile unsigned char*)0x61000000)
#define LCD_VALUE   (*(volatile unsigned char*)0x61000002)
#endif

void P8_Initialize(void)
{
    // Reset
#if (BOARD == CQFM3DUINO)
    FM3_GPIO->PFR0 &= 0xFDFF;   // P09 GPIO
    FM3_GPIO->DDR0 |= 0x0200;   // P09 output
#else
    FM3_GPIO->ADE &= 0xFFF7;    // P13 NOT Analog
    FM3_GPIO->PFR1 &= 0xFFF7;   // P13 GPIO
    FM3_GPIO->DDR1 |= 0x0008;   // P13 output
#endif
}

void LCD_nRESET(int n)
{
#if (BOARD == CQFM3DUINO)
    if (n)
        FM3_GPIO->PDOR0 |= 0x0200;
    else
        FM3_GPIO->PDOR0 &= 0xFDFF;
#else
    if (n)
        FM3_GPIO->PDOR1 |= 0x0008;
    else
        FM3_GPIO->PDOR1 &= 0xFFF7;
#endif
}

void P8_LCD_cmd(volatile unsigned short index)
{
#if (BOARD == CQFM3DUINO)
    LCD_ADDRESS = (index << 0);
    LCD_ADDRESS = (index << 8);
#else
    LCD_ADDRESS = (unsigned char)(index >> 8);
    LCD_ADDRESS = (unsigned char)(index >> 0);
#endif
    return;
}

void P8_LCD_dat(volatile unsigned short data)
{
#if (BOARD == CQFM3DUINO)
    LCD_VALUE = (data << 0);
    LCD_VALUE = (data << 8);
#else
    LCD_VALUE = (unsigned char)(data >> 8);
    LCD_VALUE = (unsigned char)(data >> 0);
#endif
}

unsigned short P8_LCD_read_dat(volatile unsigned short index)
{
    volatile unsigned short data;
#if (BOARD == CQFM3DUINO)
    LCD_ADDRESS = (index << 0);
    LCD_ADDRESS = (index << 8);
    data = (LCD_VALUE) & 0xff00;
    data += (LCD_VALUE) >> 8;
#else
    LCD_ADDRESS = (unsigned char)(index >> 8);
    LCD_ADDRESS = (unsigned char)(index >> 0);
    data = (unsigned short)LCD_VALUE;
    data = (data << 8) + (unsigned short)LCD_VALUE;
#endif
    return data;
}
//--//
